package com.gsk.europepharma.salesReport.struts.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRAdmPdtDAO;

public class ListePresentationsMarche  extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		HttpSession sessionObject = request.getSession();
		
		tbSpBoSRAdmPdtDAO admPdtDao;
		Session session;
		Query req;
		ArrayList result;
		Iterator iter;
		
		String lib_orig = request.getParameter("lib_orig"); 
		
		String cs_prod_hier_string = request.getParameter("cs_prod_hier"); 
		Integer cs_prod_hier = Integer.parseInt(cs_prod_hier_string);
		
		JSONArray presentations = new JSONArray();
		
		int cs_adm_pdt = 0;
		if (sessionObject.getAttribute("cs_adm_pdt") != null && Integer.parseInt(sessionObject.getAttribute("cs_adm_pdt").toString()) != 0) {
			cs_adm_pdt = Integer.parseInt(sessionObject.getAttribute("cs_adm_pdt").toString());
		}
			
		// Liste des presentations  
		admPdtDao = new tbSpBoSRAdmPdtDAO();
		session = admPdtDao.getSession();
		if (lib_orig.equals("GD")) {
			req = session.getNamedQuery(admPdtDao.QUERY_LISTE_PRESENTATIONS_MARCHE_DOM);
		} else {
			req = session.getNamedQuery(admPdtDao.QUERY_LISTE_PRESENTATIONS_MARCHE);
		}
		req.setInteger("cs_adm_pdt", cs_adm_pdt);
		req.setInteger("cs_prod_hier", cs_prod_hier);
		req.setString("lib_orig", lib_orig);
		result = new ArrayList(req.list());
		iter = result.iterator();
		while (iter.hasNext()) {
			Object[] tmp = (Object[]) iter.next();
			JSONObject presentation = new JSONObject();
			presentation.put("cs_adm_prstn", Integer.parseInt(tmp[0].toString()));
			presentation.put("cs_adm_rgpt", Integer.parseInt(tmp[1].toString()));
			presentation.put("origine", tmp[2].toString());
			presentation.put("marche", tmp[3].toString());
			presentation.put("cs_prod_hier", Integer.parseInt(tmp[4].toString()));
			presentation.put("cs_presentation", Integer.parseInt(tmp[5].toString()));
			presentation.put("presentation", tmp[6].toString());
			if (tmp[7] != null) {
				presentation.put("cip", tmp[7].toString());
			} else {
				presentation.put("cip", "");
			}
			presentation.put("affect", Boolean.parseBoolean(tmp[8].toString()));
			presentations.add(presentation);
		}
			
		
		// Valeurs envoyees dans l'action 
		HashMap hm = new HashMap();
        hm.put("presentations", presentations);
        
        JSONObject json = JSONObject.fromObject(hm);

        response.addHeader("Content-Type", "application/json");
        response.getOutputStream().write(json.toString().getBytes("UTF-8"));
        response.getOutputStream().flush();

		return null;
	}

}
